/*全国省份*/
function DoctorProvince() {
    $.ajax({
        url: "findByProvince",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            if (data.code == 500) {

            } else {
                var str = null;
                for (var i = 0; i < data.ProvinceList.length; i++) {
                    str += "<option value='" + data.ProvinceList[i].provinceID + "'>" + data.ProvinceList[i].provinceName + "</option>";
                }
            }
            $("#Province").append(str);
        }
    });
}

/*全国县区*/
function changeCity() {
    //城市编号
    var cityID = $("#City").val();
    var DataSources = {
        "cityID": cityID
    };
    $.ajax({
        url: "findByCountry",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 500) {

            } else {
                var str = null;
                for (var i = 0; i < data.CountryList.length; i++) {
                    str += "<option value='" + data.CountryList[i].countryID + "'>" + data.CountryList[i].countryName + "</option>";
                }
            }
            $("#Country").empty();
            $("#Country").append(str);
        }
    });
}

/*全国城市*/
function changeProvince() {
    //省份编号
    var provinceID = $("#Province").val();
    var DataSources = {
        "provinceID": provinceID
    };
    if (provinceID == "0") {
        $("#City").empty();
        $("#Country").empty();
        $("#City").append("<option value='0'>=====请选择城市=====</option>");
        $("#Country").append("<option value='0'>=====请选择县区=====</option>");
    } else {
        $.ajax({
            url: "findByCity",
            type: "POST",
            dataType: "json",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify(DataSources),
            success: function (data) {
                if (data.code == 500) {

                } else {
                    var str = null;
                    for (var i = 0; i < data.CityList.length; i++) {
                        str += "<option value='" + data.CityList[i].cityID + "'>" + data.CityList[i].cityName + "</option>";
                    }
                }
                $("#City").empty();
                $("#City").append(str);
                changeCity();
            }
        });
    }
}

function orderData() {
    //县区编号
    var countryID = $("#Country option:selected").val();
    var DataSources = {
        "countryID": countryID
    };
    $.ajax({
        url: "findOrderByOrderUsersData",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 500) {
                Swal.fire("温馨提示", "附近没有订单", "warning");
                $("#accordion").html("");
            } else {
                var code = '';
                for (var i = 0; i < data.order_list.length; i++) {
                    code += "  <div class=\"card\">\n" +
                        "                                <div class=\"card-header\">\n" +
                        "                                    <a class=\"heading-title collapsed\" data-toggle=\"collapse\" href=\"#collapseOne" + data.order_list[i].orderID + "\"\n" +
                        "                                       role=\"button\" aria-expanded=\"false\" onclick=\"checkData(" + data.order_list[0].orderID + ")\">\n" +
                        "                                        " + data.order_list[i].orderName + "\n" +
                        "                                    </a>\n" +
                        "                                </div>\n" +
                        "\n" +
                        "                                <div id=\"collapseOne" + data.order_list[i].orderID + "\" class=\"collapse\" data-parent=\"#accordion\">\n" +
                        "\n" +
                        "                                </div>\n" +
                        "                            </div>";
                }
                $("#accordion").html("");
                $("#accordion").append(code);
            }
        }
    });

}

function checkData(orderID) {
    var DataSources = {
        "orderID": orderID
    };
    $.ajax({
        url: "findOrderProductByOrderProductData",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 500) {
                Swal.fire("温馨提示", "附近没有订单", "warning");
            } else {
                var code = '';
                for (var i = 0; i < data.orderProduct_list.length; i++) {
                    code += "                             <div class=\"list-view-layout1\">\n" +
                        "                                                            <div class=\"product-box-layout3\">\n" +
                        "                                                                <div class=\"item-img\">\n" +
                        "                                                                    <a href=web-single-product.html?id=" + data.orderProduct_list[i].recProductID + "><img\n" +
                        "                                                                          style='width: 275px;height: 195px;' src='upload/" + data.orderProduct_list[i].recycleProductEntity.recProductImage + "' alt=\"Product\"></a>\n" +
                        "                                                                </div>\n" +
                        "                                                                <div class=\"product-info\">\n" +
                        "                                                                    <div class=\"item-content\">\n" +
                        "                                                                        <h3 class=\"item-title\"><a href=web-single-product.html?id=" + data.orderProduct_list[i].recProductID + ">" + data.orderProduct_list[i].recycleProductEntity.recProductName + "\n" +
                        "                                                                            </a><span></span></h3>\n" +
                        "                                                                        <ul class=\"entry-meta\">\n" +
                        "                                                                            <li><i class=\"far fa-clock\"></i>" + data.orderProduct_list[i].orderProductDate + "</li>\n" +
                        "                                                                            </li>\n" +
                        "                                                                        </ul>\n" +
                        "                                                                        <ul class=\"item-condition\">\n" +

                        "                                                                        </ul>\n" +
                        "                                                                        <div class=\"btn-group\">\n" +
                        "                                                                        </div>\n" +
                        "                                                                    </div>\n" +
                        "                                                                    <div class=\"item-right\">\n" +
                        "                                                                        <div class=\"item-price\">\n" +
                        "                                                                            <span class=\"currency-symbol\">$</span>\n" +
                        "                                                                          " + data.orderProduct_list[i].recycleProductEntity.recProductPrice + "\n" +
                        "                                                                        </div>\n" +
                        "                                                                    </div>\n" +
                        "                                                                </div>\n" +
                        "                                                            </div>\n" +
                        "                                                        </div>\n";
                }
                var detail = '';
                detail += "                                                        <div class=\"pagination-layout1\">\n" +
                    "                                                            <div class=\"btn-prev disabled\">\n" +
                    "\n" +
                    "                                                            </div>\n" +
                    "                                                            <div class=\"page-number\">\n" +
                    "\n" +
                    "                                                            </div>\n" +
                    "                                                            <div class=\"btn-next\">\n" +
                    "                                                                <a onclick='orderUpdate(" + orderID + ")'>我要接单<i class=\"fas fa-angle-double-right\"></i></a>\n" +
                    "                                                            </div>\n" +
                    "                                                        </div>";
                $("#collapseOne" + orderID).html("");
                $("#collapseOne" + orderID).append(code);
                $("#collapseOne" + orderID).append(detail);
            }
        }
    });
}

function orderUpdate(orderID) {
    var DataSources = {
        "orderID": orderID,
        "orderState": 2
    };
    $.ajax({
        url: "updateOrderByOrderState",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 200) {
                updateOrderByUsersID(orderID);
            } else {
                Swal.fire("温馨提示", "接单失败", "error");
            }
        }
    });
}

function updateOrderByUsersID(orderID) {
    var DataSources = {
        "orderID": orderID,
    };
    $.ajax({
        url: "updateOrderByUsersID",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(DataSources),
        success: function (data) {
            if (data.code == 200) {
                Swal.fire("温馨提示", "接单成功", "success");
                orderData();
            } else {
                Swal.fire("温馨提示", "接单失败", "error");
            }
        }
    });
}

$(function () {
    var DATA = '';
    $.ajax({
        url: "JudgeLogin_3",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            if (data.code == 500) {
                DATA += " <li class=\"header-btn\">\n" +
                    "                                    <a href=\"web-login.html\" class=\"item-btn\"><i class=\"fas fa-plus\"></i>登录</a>\n" +
                    "                                </li>"
                Swal.fire("温馨提示", "请先登录", "warning");
                window.location.href = "web-user-404.html";
            } else {
                DATA += " <li class=\"header-login-icon\">\n" +
                    "                                    <a href=\"web-user-account.html\" class=\"color-light\" data-toggle=\"tooltip\"\n" +
                    "                                       data-placement=\"top\" title=\"Login/Register\">\n" +
                    "                                        <i class=\"far fa-user\">我的</i>\n" +
                    "                                    </a>\n" +
                    "                                </li>\n" +
                    "                                <li class=\"header-btn\">\n" +
                    "                                    <a  onclick='tuichu_1()' class=\"item-btn\"><i class=\"fas fa-plus\"></i>退出</a>\n" +
                    "                                </li>";
                DoctorProvince();
            }
            $("#userData").html("");
            $("#userData").append(DATA);
        }
    });
});